package Scuttlebutt;

import java.awt.event.*;
import javax.swing.*;
import java.awt.Container;
import java.awt.Insets;
import java.awt.Dimension;
import java.io.*;
import java.lang.Integer;
import glue.*;

public class NewProjectDlg extends JDialog 
{
	Scuttlebutt owner = null;
	
	JTextField projectName = null;

	JTextField ftpAddress = null;
	JTextField port = null;
	JTextField username = null;
	JPasswordField password = null;	
	
	JTextField path = null;
	
	File projectPath = null;
	
	BrowseButtonAction browseAction = null;
	CreateButtonAction createAction = null;
	CancelButtonAction cancelAction = null;
	
	boolean bState = false;
	
	JButton create = null;
	JButton cancel = null;
	JButton browse = null;
	
	JCheckBox synchronizeBox = null;
	
	public class BrowseButtonAction extends AbstractAction {
        public BrowseButtonAction(String text, Integer mnemonic) {
            super(text, null);
            putValue(MNEMONIC_KEY, mnemonic);
        }
        public void actionPerformed(ActionEvent e) {
	       
			JFileChooser chooser = new JFileChooser();
//			chooser.addChoosableFileFilter(new SuperFileFilter(new String("Project"), new String("prj")));
			chooser.setCurrentDirectory(new File("C:"+ File.separatorChar + "web"));

			chooser.setMultiSelectionEnabled(false);
			chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY );
			int returnVal = chooser.showSaveDialog(owner);
			if(returnVal == JFileChooser.APPROVE_OPTION){
				projectPath = chooser.getSelectedFile();
				path.setText(projectPath.getAbsolutePath());
				
			}
        }
    }		
    
	public class CancelButtonAction extends AbstractAction {
        public CancelButtonAction(String text, Integer mnemonic) {
            super(text, null);
            putValue(MNEMONIC_KEY, mnemonic);
        }
        public void actionPerformed(ActionEvent e) {
	        setVisible(false);
	        bState = false;

        }
    }	
    
	public class CreateButtonAction extends AbstractAction {
        public CreateButtonAction(String text, Integer mnemonic) {
            super(text, null);
            putValue(MNEMONIC_KEY, mnemonic);
        }
        public void actionPerformed(ActionEvent e) {
	        bState = true;
	        projectName.getText().trim();
	        path.getText().trim();
	        if(projectName.getText().length() == 0){
		        JOptionPane.showMessageDialog(null, "Project Name cannot be blank", "Error", JOptionPane.ERROR_MESSAGE);
		        projectName.grabFocus();
		        return;
	        }
	        else if (path.getText().length() == 0){
		        JOptionPane.showMessageDialog(null, "Path cannot be blank", "Error", JOptionPane.ERROR_MESSAGE);
		        path.grabFocus();
		        return;
	        }
	        
	        if(projectPath == null){
		        projectPath = new File(path.getText());
	        }
	        
       		setVisible(false);
        }
    }	
    

	public NewProjectDlg(Scuttlebutt parent, String title, boolean modal)
	{
		super(parent, title, modal);
		this.owner = parent;
		init();	
		setLocationRelativeTo(owner);
		projectName.grabFocus();
	}
	
	private void init(){
		Container container=getContentPane();
		container.setLayout(null);
		setupDialog(container);
	}
	
	public void setupDialog(Container pane){
		browseAction = new BrowseButtonAction("Browse...",new Integer(KeyEvent.VK_B));
		createAction = new CreateButtonAction("Create",new Integer(KeyEvent.VK_O));
		cancelAction = new CancelButtonAction("Cancel",new Integer(KeyEvent.VK_C));
		
		Insets insets = pane.getInsets();
		int headspace = 5;
		int between = 5;
		int left_margin = 20;
		
		JLabel name = new JLabel("Project Name:");
		pane.add(name);
		Dimension size = name.getPreferredSize();
		name.setBounds(insets.left + left_margin, insets.top + headspace, size.width, size.height);
		
		int x = insets.left + left_margin + name.getSize().width;
		projectName = new JTextField(15);
		pane.add(projectName);
		size = projectName.getPreferredSize();
		projectName.setBounds(x + between, insets.top + headspace, size.width, size.height);

		headspace += name.getSize().height + 5;
		JLabel ftpl = new JLabel("FTP:");
		pane.add(ftpl);
		size = ftpl.getPreferredSize();
		ftpl.setBounds(x - ftpl.getPreferredSize().width, insets.top + headspace, size.width, size.height);
		
		
		ftpAddress = new JTextField(15);
		pane.add(ftpAddress);
		size = ftpAddress.getPreferredSize();
		ftpAddress.setBounds(x + between, insets.top + headspace, size.width, size.height);
		
		headspace += name.getSize().height + 5;
		JLabel portl = new JLabel("Port:");
		pane.add(portl);
		size = portl.getPreferredSize();
		portl.setBounds(x - portl.getPreferredSize().width, insets.top + headspace, size.width, size.height);
		
		port = new JTextField("21",5);
		pane.add(port);
		size = port.getPreferredSize();
		port.setBounds(x + between, insets.top + headspace, size.width, size.height);

		headspace += name.getSize().height + 5;
		JLabel usernamel = new JLabel("Username:");
		pane.add(usernamel);
		size = usernamel.getPreferredSize();
		usernamel.setBounds(x - usernamel.getPreferredSize().width, insets.top + headspace, size.width, size.height);
		
		username = new JTextField(15);
		pane.add(username);
		size = username.getPreferredSize();
		username.setBounds(x + between, insets.top + headspace, size.width, size.height);

		headspace += name.getSize().height + 5;
		JLabel passwordl = new JLabel("Password:");
		pane.add(passwordl);
		size = passwordl.getPreferredSize();
		passwordl.setBounds(x - passwordl.getPreferredSize().width, insets.top + headspace, size.width, size.height);
		
		password = new JPasswordField(15);
		pane.add(password);
		size = password.getPreferredSize();
		password.setBounds(x + between, insets.top + headspace, size.width, size.height);

		
		headspace += name.getSize().height + 5;
		JLabel pathl = new JLabel("Path:");
		pane.add(pathl);
		size = pathl.getPreferredSize();
		pathl.setBounds(x - pathl.getPreferredSize().width, insets.top + headspace, size.width, size.height);
		
		path = new JTextField(15);
		pane.add(path);
		size = path.getPreferredSize();
		path.setBounds(x + between, insets.top + headspace, size.width, size.height);

		browse = new JButton(browseAction);
		pane.add(browse);
		size = browse.getPreferredSize();
		browse.setBounds(x + between + path.getPreferredSize().width + between, insets.top + headspace, size.width, size.height);

		headspace += browse.getSize().height + 5;
		synchronizeBox = new JCheckBox("Synchronize (recommended)", true);
		pane.add(synchronizeBox);
		size = synchronizeBox.getPreferredSize();
		synchronizeBox.setBounds(x, insets.top + headspace, size.width, size.height);
		
		headspace += browse.getSize().height + 5;
		
		cancel = new JButton(cancelAction);
		pane.add(cancel);
		size = cancel.getPreferredSize();
		cancel.setBounds(x + between + path.getPreferredSize().width + between + browse.getPreferredSize().width - size.width, insets.top + headspace, size.width, size.height);
		
		create = new JButton(createAction);
		pane.add(create);
		size = create.getPreferredSize();
		create.setBounds(x + between + path.getPreferredSize().width + browse.getPreferredSize().width - cancel.getPreferredSize().width/* - between*/ - create.getPreferredSize().width, insets.top + headspace, size.width, size.height);

		headspace += create.getPreferredSize().height + 5;
				
		setSize(new Dimension(x + between + path.getPreferredSize().width + between + browse.getPreferredSize().width + left_margin, headspace + create.getPreferredSize().height + 5));
		
	}
	
    public boolean getState(){
	    return bState;
    }
    
    public String getProjectName(){
	    return projectName.getText();
    }
    
    public File getProjectPath(){
	    return projectPath;
    }
    
	public String getAddress(){
		return ftpAddress.getText();
	}
	
	public int getPort(){
		Integer i = new Integer(port.getText());
		return i.intValue();
	}
	
	public String getUsername(){
		return username.getText();
	}
	
	public String getPassword(){
		
		return new String(password.getPassword());
	}
	
}

